package xjnu.edu.dao;

import java.security.interfaces.RSAKey;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.enterprise.inject.New;

import xjnu.edu.entity.Friends;

public class FriendDao {
	private static String url="jdbc:mysql://localhost:3306/struct2?user=root&password=root";
	
	public void insertFriends(Friends friends) {
		// TODO Auto-generated method stub
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection conn = DriverManager.getConnection(url);
			String sql = "insert into friends values(?,?,?,?,?,?,?)";
			PreparedStatement prst = conn.prepareStatement(sql);
			prst.setString(1, friends.getUserName());
			prst.setString(2, friends.getName());
			prst.setString(3, friends.getPhone());
			prst.setString(4, friends.getEmail());
			prst.setString(5, friends.getWorkplace());
			prst.setString(6, friends.getPlace());
			prst.setString(7, friends.getQQ());
			prst.execute();
			prst.close();
			conn.close();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public List<Friends> findAllFriends(String userName) {
		// TODO Auto-generated method stub
		List<Friends> listfrFriends = new ArrayList<Friends>();
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection conn = DriverManager.getConnection(url);
			String sql = "select * from friends where userName=?";
			PreparedStatement prst = conn.prepareStatement(sql);
			prst.setString(1, userName);
			ResultSet rs = prst.executeQuery();
			while (rs.next()) {
				Friends friends = new Friends();
				friends.setUserName(rs.getString("userName"));
				friends.setName(rs.getString("name"));
				friends.setPhone(rs.getString("phone"));
				friends.setEmail(rs.getString("email"));
				friends.setWorkplace(rs.getString("workplace"));
				friends.setPlace(rs.getString("place"));
				friends.setQQ(rs.getString("QQ"));
				listfrFriends.add(friends);
			}
			prst.close();
			conn.close();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return listfrFriends;
	}

	public Friends getFriendMessage_Phone(String phone) {
		// TODO Auto-generated method stub
		Friends friends = new Friends();
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection conn = DriverManager.getConnection(url);
			String sql = "select * from friends where phone=?";
			PreparedStatement prst = conn.prepareStatement(sql);
			prst.setString(1, phone);
			ResultSet rs = prst.executeQuery();
			while (rs.next()) {
				friends.setName(rs.getString("name"));
				friends.setPhone(rs.getString("phone"));
				friends.setEmail(rs.getString("email"));
				friends.setWorkplace(rs.getString("workplace"));
				friends.setPlace(rs.getString("place"));
				friends.setQQ(rs.getString("QQ"));
				return friends;
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return friends;
	}

	public void updateFriendMessage(String name, String phone, String email,
			String workplcae, String place, String QQ) {
		// TODO Auto-generated method stub
		Friends friends = new Friends();
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection conn = DriverManager.getConnection(url);
			String sql = "update friends set phone=?,email=?,workplace=?" +
					", place=?,QQ=? where name=?";
			PreparedStatement prst = conn.prepareStatement(sql);
			prst.setString(1, phone);
			prst.setString(2, email);
			prst.setString(3, workplcae);
			prst.setString(4, place);
			prst.setString(5, QQ);
			prst.setString(6, name);
			prst.execute();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	public void deleteFriendMessage(String phone) {
		// TODO Auto-generated method stub
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection conn = DriverManager.getConnection(url);
			String sql = "delete from friends where phone=?";
			PreparedStatement prst = conn.prepareStatement(sql);
			prst.setString(1, phone);
			prst.execute();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
